package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired(required = false)
    private DishService dishService;
    @Autowired(required = false)
    private DishFlavorService dishFlavorService;

    /**
     * 增加菜品
     *
     * @param session
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(HttpSession session, @RequestBody DishDto dishDto) {
        //1. 获取当前登录对象
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateUser(empId);
        dishDto.setUpdateTime(LocalDateTime.now());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageList = dishService.page(page, pageSize, name);
        return R.success(pageList);
    }

    /**
     * 根据菜品id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") long id) {
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     *
     * @param dishDto 页面传递过来的参数包含菜品与口味
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto, HttpSession session) {
        //获取当前登录登录者的Id
        long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");

    }

    /**
     * 根据条件查询对应的菜品管理
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> List(Long categoryId, Integer status) {
       List<DishDto>dishList=dishService.findByCategoryId(categoryId ,status);
       return R.success(dishList);
    }

    /**
     * 删除菜品(可批量)
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional
    public R<String> delete(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public  R<String> updateStatus(@RequestParam("ids") List<Long> ids ,@PathVariable("status") Integer status){
        dishService.updateStatus(ids,status);
        return R.success("修改状态成功");
    }

}
